﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using D2DEngine.Scene;
using Microsoft.Xna.Framework.Graphics;
using D2DEngine.Core;

namespace D2DEngine
{
    public class D2DWorldLimitComponent : D2DComponent
    {
        bool hasCollision;
        D2DSceneObject sceneObject;

        /// <summary>
        /// Did the object hit the world limit ?
        /// </summary>
        public bool HasCollision
        {
            get { return this.hasCollision; }
            set { this.hasCollision = value; }
        }

        /// <summary>
        /// The callback method when a collision occurs
        /// </summary>
        public OnWorldLimitDelegate OnWorldLimit;

        public D2DWorldLimitComponent()
        {
            this.OnWorldLimit = new OnWorldLimitDelegate(OnWorldLimitMethod);
        }

        /// <summary>
        /// The method called when the object hits the world limit
        /// </summary>
        /// <param name="obj"></param>
        protected void OnWorldLimitMethod()
        {
        }

        public override void OnRegister()
        {
            this.sceneObject = this.owner as D2DSceneObject;
            
            base.OnRegister();
        }

        /// <summary>
        /// Updates the world limit component
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            hasCollision = false;

            if (!D2DSceneManager.Instance.CurrentScene.BoundingBox.Contains(this.sceneObject.BoundingBox))
            {
                hasCollision = true;
                OnWorldLimit();
            }
        }

        protected override void CopyTo(D2DComponent component)
        {
            base.CopyTo(component);

            D2DWorldLimitComponent comp = (D2DWorldLimitComponent)component;
            comp.OnWorldLimit = this.OnWorldLimit;
        }
        
    }

    public delegate void OnWorldLimitDelegate();

}
